package com.t2two.example.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.t2two.example.common.entity.AddressResolutionDto;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddressUtil {

    public AddressUtil() {}


    /**
     * 从地址串中解析提取出省市区等信息
     * @param address   地址信息
     * @return          解析后的地址Map
     */
    public static Map<String,Object> addressResolution(String address){
        if(StringUtils.isBlank(address)){
            System.out.println("address "+address);
            return Collections.emptyMap();
        }

        Map<String,Object> map = new HashMap<>(16);

        return map;
    }

    /**
     * 根据地址获取解析后的地址对象
     * @param address   解析前地址Str
     * @return          解析后地址对象
     */
//    public static WSSsdrAddress resolveAddress(String address) {
//        if (CharSequenceUtil.isBlank(address)) {
//            throw new BaseException("客户联系地址出错");
//        }
//        Map<String, String> addressMap = addressResolution(address);
//        return new WSSsdrAddress(addressMap.get("province"), addressMap.get("city"), addressMap.get("county"), addressMap.get("address"));
//    }

    public static AddressResolutionDto addressResolutions(String address) {
        if (StringUtils.isBlank(address)) {
            return null;
        }
        address = address.replace("\n", "");
//        String allProvince = "北京|天津|上海|重庆|河北|山西|辽宁|吉林|黑龙江|江苏|浙江|安徽|福建|江西|山东|河南|湖北|湖南|广东|海南|四川|贵州|云南|陕西|甘肃|青海|台湾|内蒙古|广西|西藏|宁夏|新疆|香港|澳门";
//        String allProvinceFullName = "北京市|天津市|上海市|重庆市|河北省|山西省|辽宁省|吉林省|黑龙江省|江苏省|浙江省|安徽省|福建省|江西省|山东省|河南省|湖北省|湖南省|广东省|海南省|四川省|贵州省|云南省|陕西省|甘肃省|青海省|台湾省|内蒙古自治区|广西壮族自治区|西藏自治区|宁夏回族自治区|新疆维吾尔自治区|香港特别行政区|澳门特别行政区";



        // 百度到的正则
//        String regex="(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        // 修改后的正则
        String regex = "(?<province>北京|上海|天津|重庆|北京市|上海市|天津市|重庆市|[^省]+省|.+自治区|[^澳门]+澳门|[^香港]+香港)?(?<city>[^自治州]+自治州|[^特别行政区]+特别行政区|[^市]+市|.*?地区|.*?行政单位|.+盟|市辖区|直辖县|[^县]+县)(?<county>[^县]+县|[^市]+市|[^区]+区|[^乡]+乡|[^镇]+镇|[^村]+村|.+场|.+旗|.+海域|.+岛)?(?<town>[^乡]+乡|[^镇]+镇|[^村]+村|[^街道]+街道|[^社区]+社区|[^所]+所|[^办事处]+办事处|[^服务中心]+服务中心|[^开发区]+开发区|.+社|.+居委会)?(?<village>[^村]+村|[^组]+组|[^居委会]+居委会|[^村委会]+村委会|[^居民委员会]+居民委员会|[^社区]+社区|.+小区|.+园|.+区)?(?<address>.*)";
//        String regex = "(?<province>北京市|上海市|天津市|重庆市|北京|上海|天津|重庆|[^省]+省|.+自治区|[^澳门]+澳门|[^香港]+香港)?(?<city>(?:[^自治州]+自治州|[^特别行政区]+特别行政区|[^市]+市|.*?地区|.*?行政单位|.+盟|市辖区|[^县]+县))?(?<county>[^县]+县|[^市]+市|[^区]+区|[^乡]+乡|[^镇]+镇|[^村]+村|.+场|.+旗|.+海域|.+岛)?(?<town>[^乡]+乡|[^镇]+镇|[^村]+村|[^街道]+街道|[^社区]+社区|[^所]+所|[^办事处]+办事处|[^服务中心]+服务中心|[^开发区]+开发区|.+社|.+居委会)?(?<village>[^村]+村|[^组]+组|[^居委会]+居委会|[^村委会]+村委会|[^居民委员会]+居民委员会|[^社区]+社区|.+小区|.+园|.+区)?(?<address>.*)";
//        String regex = "(?<province>北京|上海|天津|重庆|北京市|上海市|天津市|重庆市|[\\u4e00-\\u9fa5]{2,}(?:省|自治区|市)|[\\u4e00-\\u9fa5]{3,}(?:特别行政区|自治区|市))?(?<city>(?:[\\u4e00-\\u9fa5]{2,}(?:自治州|盟|地区|行政单位)|[\\u4e00-\\u9fa5]{2,}(?:市辖区|县级市|县)))(?<county>[^县]+县|[^市]+市|[^区]+区|[^乡]+乡|[^镇]+镇|[^村]+村|.+场|.+旗|.+海域|.+岛)?(?<town>[^乡]+乡|[^镇]+镇|[^村]+村|[^街道]+街道|[^社区]+社区|[^所]+所|[^办事处]+办事处|[^服务中心]+服务中心|[^开发区]+开发区|.+社|.+居委会)?(?<village>[^村]+村|[^组]+组|[^居委会]+居委会|[^村委会]+村委会|[^居民委员会]+居民委员会|[^社区]+社区|.+小区|.+园|.+区)?(?<address>.*)";
// 更新的正则表达式
//        String regex = "(?<province>北京|上海|天津|重庆|北京市|上海市|天津市|重庆市|[\\u4e00-\\u9fa5]{2,}(?:省|自治区|市)|[\\u4e00-\\u9fa5]{3,}(?:特别行政区|自治区|市))?(?<city>(?:(?!省|自治区|市).)*?(?:自治州|盟|地区|行政单位|[\\u4e00-\\u9fa5]{2,}(?:市辖区|县级市|县)))(?<county>[^县]+县|[^市]+市|[^区]+区|[^乡]+乡|[^镇]+镇|[^村]+村|.+场|.+旗|.+海域|.+岛)?(?<town>[^乡]+乡|[^镇]+镇|[^村]+村|[^街道]+街道|[^社区]+社区|[^所]+所|[^办事处]+办事处|[^服务中心]+服务中心|[^开发区]+开发区|.+社|.+居委会)?(?<village>[^村]+村|[^组]+组|[^居委会]+居委会|[^村委会]+村委会|[^居民委员会]+居民委员会|[^社区]+社区|.+小区|.+园|.+区)?(?<address>.*)";
// 再次更新的正则表达式
//        String regex = "(?<province>北京|上海|天津|重庆|北京市|上海市|天津市|重庆市|[\\u4e00-\\u9fa5]{2,}(?:省|自治区|市)|[\\u4e00-\\u9fa5]{3,}(?:特别行政区|自治区|市))?(?<city>(?:(?!省|自治区|市).)*?(?:自治州|盟|地区|行政单位|[\\u4e00-\\u9fa5]{2,}(?:市辖区|县级市|县)))(?<county>[^县]+县|[^市]+市|[^区]+区|[^乡]+乡|[^镇]+镇|[^村]+村|.+场|.+旗|.+海域|.+岛)?(?<town>[^乡]+乡|[^镇]+镇|[^村]+村|[^街道]+街道|[^社区]+社区|[^所]+所|[^办事处]+办事处|[^服务中心]+服务中心|[^开发区]+开发区|.+社|.+居委会)?(?<village>[^村]+村|[^组]+组|[^居委会]+居委会|[^村委会]+村委会|[^居民委员会]+居民委员会|[^社区]+社区|.+小区|.+园|.+区)?(?<address>.*)";
        //2、创建匹配规则
        Matcher m = Pattern.compile(regex).matcher(address);
        String province;
        String city;
        String county;
        String town;
        String village;
        String detailAddress;
        AddressResolutionDto dto = new AddressResolutionDto();

        while (m.find()) {
            //加入省
            province = m.group("province");
            dto.setProvince(province == null ? "" : province.trim());
            //加入市
            city = m.group("city");
            dto.setCity(city == null ? "" : city.trim());
            //加入区
            county = m.group("county");
            dto.setCounty(county == null ? "" : county.trim());
            // 加入乡镇
            town = m.group("town");
            dto.setTown(town == null ? "" : town.trim());
            // 加入村庄
            village = m.group("village");
            dto.setVillage(village == null ? "" : village.trim());

            //详细地址
            detailAddress = m.group("address");
            dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
        }
        if ("北京".equals(dto.getProvince()) || "北京市".equals(dto.getProvince())) {
            dto.setProvince("北京市");
        }else if ("天津".equals(dto.getProvince()) || "天津市".equals(dto.getProvince())) {
            dto.setProvince("天津市");
        }else if ("上海".equals(dto.getProvince()) || "上海市".equals(dto.getProvince())) {
            dto.setProvince("上海市");
        }else if ("重庆".equals(dto.getProvince()) || "重庆市".equals(dto.getProvince())) {
            dto.setProvince("重庆市");
            if(StringUtils.isNotBlank(dto.getCity()) && dto.getCity().length()>1){
                if(dto.getCity().toLowerCase().endsWith("县")){
                    dto.setCounty(dto.getCity());
                    dto.setCity("县辖区");
                }
            }
        }

        if(StringUtils.isBlank(dto.getProvince()) && StringUtils.isBlank(dto.getCity()) && StringUtils.isBlank(dto.getCounty())&& StringUtils.isBlank(dto.getTown()) && StringUtils.isBlank(dto.getVillage())){
            if("北京".equals(address) || "北京市".equals(address)){
                dto.setProvince("北京市");
            }else if("上海".equals(address) || "上海市".equals(address)){
                dto.setProvince("上海市");
            }else if("天津".equals(address) || "天津市".equals(address)){
                dto.setProvince("天津市");
            }else if("重庆".equals(address) || "重庆市".equals(address)){
                dto.setProvince("重庆市");
                System.out.println("aaaaaaa");
            }
        }else if(StringUtils.isBlank(dto.getProvince()) && StringUtils.isNotBlank(dto.getCity()) && StringUtils.isBlank(dto.getCounty())&& StringUtils.isBlank(dto.getTown()) && StringUtils.isBlank(dto.getVillage())){
            if("北京".equals(dto.getCity()) || "北京市".equals(dto.getCity())){
                dto.setProvince("北京市");
            }else if("上海".equals(dto.getCity()) || "上海市".equals(dto.getCity())){
                dto.setProvince("上海市");
            }else if("天津".equals(dto.getCity()) || "天津市".equals(dto.getCity())){
                dto.setProvince("天津市");
            }else if("重庆".equals(dto.getCity()) || "重庆市".equals(dto.getCity())){
                dto.setProvince("重庆市");
                System.out.println("dddddd");
            }
        }
        if(StringUtils.isBlank(dto.getProvince()) && StringUtils.isNotBlank(dto.getCity())){
            if("北京".equals(dto.getCity()) || "北京市".equals(dto.getCity())){
                dto.setProvince("北京市");
                dto.setCity("市辖区");
            }else if("上海".equals(dto.getCity()) || "上海市".equals(dto.getCity())){
                dto.setProvince("上海市");
                dto.setCity("市辖区");
            } else if("天津".equals(dto.getCity()) || "天津市".equals(dto.getCity())){
                dto.setProvince("天津市");
                dto.setCity("市辖区");
            }else if("重庆".equals(dto.getCity()) || "重庆市".equals(dto.getCity())){
                dto.setProvince("重庆市");
                dto.setCity("市辖区");
                System.out.println("jjjjjjjjj");
            }
        }
        if(StringUtils.isNotBlank(dto.getCity()) && dto.getCity().startsWith("直辖县")){
            String substring = dto.getCity().substring(0, 3);
            String substring1 = dto.getCity().substring(3);
            dto.setCity(substring);
            if(StringUtils.isNotBlank(dto.getCounty())){
                if(StringUtils.isNotBlank(dto.getTown())){
                    if(StringUtils.isNotBlank(dto.getVillage())){
                        dto.setVillage(dto.getTown()+dto.getVillage());
                        dto.setTown(dto.getCounty());
                        dto.setCounty(substring1);
                    }else {
                        dto.setVillage(dto.getTown());
                        dto.setTown(dto.getCounty());
                        dto.setCounty(substring1);
                    }
                }else {
                    dto.setTown(dto.getCounty());
                    dto.setCounty(substring1);
                }
            }else {
                dto.setCounty(substring1);
            }
        }
        return dto;
    }
    // 全国所有省、自治区、直辖市、特别行政区 - 枚举
    private static final String all_province = "(?:北京|天津|上海|重庆|河北|山西|辽宁|吉林|黑龙江|江苏|浙江|安徽|福建|江西|山东|河南|湖北|湖南|广东|海南|四川|贵州|云南|陕西|甘肃|青海|台湾|内蒙古|广西|西藏|宁夏|新疆|香港|澳门)";
    private static final Pattern all_province_pattern = Pattern.compile(all_province);

    // 市级正则匹配规则
    private static final String city_city = "(?:[^自治州]+自治州|[^特别行政区]+特别行政区|.*?地区|.*?行政单位|.+盟|市辖区|直辖县|[^市]+市|[^县]+县)";
    private static final Pattern city_city_pattern = Pattern.compile(city_city);

    // 县级正则匹配规则（区、县等）
    private static final String country_city = "(?:[^县]+县|[^市]+市|(?!小区)[^区]+区|[^乡]+乡|[^镇]+镇|[^村]+村|.+场|.+旗|.+海域|.+岛)";
    private static final Pattern country_city_pattern = Pattern.compile(country_city);

    // 乡镇级别正则匹配规则
    private static final String town_village = "(?<town>[^风景管理处]+风景管理处|[^街道]+街道|[^苏木]+苏木|[^办事处]+办事处|[^开发区]+开发区|[^乡]+乡|[^镇]+镇|[^村]+村|[^社区]+社区|[^所]+所|[^服务中心]+服务中心|.+社|.+居委会)(?<village>[^居委会]+居委会|[^村委会]+村委会|[^居民委员会]+居民委员会|[^嘎查]+嘎查|[^生活区]+生活区|[^村]+村|[^组]+组|[^社区]+社区|.+小区|.+园|.+区)?(?<address>.*)";
    private static final Pattern town_village_pattern = Pattern.compile(town_village);

    // 重庆地级市
    private static final String chongQing5001 = "(?:万州|涪陵|渝中|大渡口|江北|沙坪坝|九龙坡|南岸|北碚|綦江|大足|渝北|巴南|黔江|长寿|江津|合川|永川|南川|壁山|铜梁|潼南|荣昌|开州|梁平|武隆)";
    private static final Pattern chongQing5001_pattern = Pattern.compile(chongQing5001);
    // 重庆县级市（石柱土家族自治县、秀山土家族苗族自治县、酉阳土家族苗族自治县、彭水苗族土家族自治县）
    private static final String chongQing5002 = "(?:石柱|秀山|酉阳|彭水)";
    private static final Pattern chongQing5002_pattern = Pattern.compile(chongQing5002);

    // 新疆县级市
    private static final String xinJiang6590 = "(?:石河子|阿拉尔|图木舒克|五家渠|北屯|铁门关|双河|可克达拉|昆玉|胡杨河|新星|白杨)";
    private static final Pattern xinJiang6590_pattern = Pattern.compile(xinJiang6590);

    // 海南地级市
    private static final String haiNanCity = "(?:海口|三亚|三沙|儋州)";
    private static final Pattern haiNanCity_pattern = Pattern.compile(haiNanCity);

    // 湖北地级市
    private static final String huBeiCity = "(?:武汉|黄石|十堰|宜昌|襄阳|鄂州|荆门|孝感|荆州|黄冈|咸宁|随州)";
    private static final Pattern huBeiCity_pattern = Pattern.compile(huBeiCity);
    // 湖北恩施土家族苗族自治州（市结尾）-恩施市太特殊，在代码中写死
    private static final String huBeiCity4228 = "(?:利川)";
    private static final Pattern huBeiCity4228_pattern = Pattern.compile(huBeiCity4228);
    // 湖北恩施土家族苗族自治州（县结尾）
    private static final String huBeiCounty4228 = "(?:建始|巴东|宣恩|咸丰|来凤|鹤峰)";
    private static final Pattern huBeiCounty4228_pattern = Pattern.compile(huBeiCounty4228);
    // 湖北直辖县（市结尾 和 神农架林区）
    private static final String huBeiCounty4290 = "(?:仙桃|潜江|天门|神农架)";
    private static final Pattern huBeiCounty4290_pattern = Pattern.compile(huBeiCounty4290);

    // 河南省地级市 （处理县直辖）
    private static final String heNanCity = "(?:郑州|开封|洛阳|平顶山|安阳|鹤壁|新乡|焦作|濮阳|许昌|漯河|三门峡|南阳|商丘|信阳|周口|驻马店)";
    private static final Pattern heNanCity_pattern = Pattern.compile(heNanCity);

    public static AddressResolutionDto addressResolutionX(String address) {
        if (StringUtils.isBlank(address)) {
            return null;
        }
        address = address.replace("\n", "").trim();
        AddressResolutionDto dto = new AddressResolutionDto();
        Matcher matcher = all_province_pattern.matcher(address);
        String provinceStart = null;
        if (matcher.find()) {
            provinceStart = matcher.group();
            String group = provinceStart;
            if("北京".equals(group) || "天津".equals(group) || "上海".equals(group) || "重庆".equals(group)){
                group += "市";
            }else if("香港".equals(group) || "澳门".equals(group)){
                group += "特别行政区";
            }else if("内蒙古".equals(group) || "西藏".equals(group)){
                group += "自治区";
            }else if("新疆".equals(group)){
                group += "维吾尔族自治区";
            }else if("广西".equals(group)){
                group += "壮族自治区";
            }else if("宁夏".equals(group)){
                group += "回族自治区";
            }else {
                group += "省";
            }
            dto.setProvince(group);
        }
        if(StringUtils.isNotBlank(provinceStart)){
            if(("北京".equals(provinceStart) || "天津".equals(provinceStart) || "上海".equals(provinceStart)) && address.length()>2){
                String address1 = address.substring(2);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("市")){
                    address1 = address1.substring(1);
                }
                if(address1.startsWith("辖区")){
                    dto.setCity("市辖区");
                    address1 = address1.substring(2);
                }else if(address1.startsWith("市辖区")){
                    dto.setCity("市辖区");
                    address1 = address1.substring(3);
                }else {
                    if(address1.length()>0){
                        dto.setCity("市辖区");
                    }
                }
                if(StringUtils.isNotBlank(address1)){
                    Matcher matcher1 = country_city_pattern.matcher(address1);
                    String countyStart = null;
                    if(matcher1.find()){
                        countyStart = matcher1.group();
                        String group = countyStart;
                        dto.setCounty(group);
                    }
                    if(StringUtils.isNotBlank(countyStart)){
                        if(countyStart.length()<address1.length()){
                            String address2 = address1.substring(countyStart.length());
                            Matcher matcher2 = town_village_pattern.matcher(address2);
                            while (matcher2.find()) {
                                // 加入乡镇
                                String town = matcher2.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher2.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher2.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }
                    }
                }
            } else if("重庆".equals(provinceStart) && address.length()>2){
                String address1 = address.substring(2);
                System.out.println("2222222222222 "+address1);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("市")){
                    address1 = address1.substring(1);
                    if(address1.startsWith("辖区")){
                        dto.setCity("市辖区");
                        address1 = address1.substring(2);
                    }else if(address1.startsWith("市辖区")){
                        dto.setCity("市辖区");
                        address1 = address1.substring(3);
                    }else {
                        if(address1.length()>0){
                            dto.setCity("市辖区");
                        }
                    }
                }else if(StringUtils.isNotBlank(address1) && address1.startsWith("县")){
                    address1 = address1.substring(1);
                    if(address1.startsWith("辖区")){
                        dto.setCity("县辖区");
                        address1 = address1.substring(2);
                    }else if(address1.startsWith("县辖区")){
                        dto.setCity("县辖区");
                        address1 = address1.substring(3);
                    }else {
                        if(address1.length()>0){
                            dto.setCity("县辖区");
                        }
                    }
                }
                System.out.println("444444444444 "+JSONObject.toJSONString(dto));
                if(StringUtils.isNotBlank(address1)){
                    if(address1.startsWith("县辖区")){
                        address1 = address1.substring(3);
                    }
                    System.out.println("jjjjjjjjjjjj "+address1);
                    Matcher matcher1 = country_city_pattern.matcher(address1);
                    String countyStart = null;
                    if(matcher1.find()){
                        countyStart = matcher1.group();
                        String group = countyStart;
                        dto.setCounty(group);
                    }
                    System.out.println("555555555555 "+JSONObject.toJSONString(dto));
                    if(StringUtils.isNotBlank(countyStart)){
                        Matcher matcher0 = chongQing5001_pattern.matcher(countyStart);
                        dto.setCity("县辖区");
                        if(matcher0.find()){
                            String group = matcher0.group();
                            if(StringUtils.isNotBlank(group)){
                                dto.setCity("市辖区");
                            }
                        }
                        System.out.println("aaaaaaaaaaa "+JSONObject.toJSONString(dto));
                        if(countyStart.length()<address1.length()){
                            Matcher matcher3 = chongQing5002_pattern.matcher(countyStart);
                            if("县辖区".equals(dto.getCity()) && matcher3.find()){
                                String group = matcher3.group();
                                // 重庆下面县级行政单位，4个自治县做特殊处理。石柱|秀山|酉阳|彭水
                                // 石柱土家族自治县、秀山土家族苗族自治县、酉阳土家族苗族自治县、彭水苗族土家族自治县
                                if("石柱".equals(group)){
                                    dto.setCounty("石柱土家族自治县");
                                }else if("秀山".equals(group)){
                                    dto.setCounty("秀山土家族苗族自治县");
                                }else if ("酉阳".equals(group)){
                                    dto.setCounty("酉阳土家族苗族自治县");
                                }else if("彭水".equals(group)){
                                    dto.setCounty("彭水苗族土家族自治县");
                                }
                            }
                            String address2 = address1.substring(countyStart.length());
                            System.out.println("bbbbbbbbbbb "+address2);
                            Matcher matcher2 = town_village_pattern.matcher(address2);
                            while (matcher2.find()) {
                                // 加入乡镇
                                String town = matcher2.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher2.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher2.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }
                    }
                }
            } else if("香港".equals(provinceStart) || "澳门".equals(provinceStart)){
                // 暂不实现
            } else if("内蒙古".equals(provinceStart) && address.length()>3){
                String address1 = address.substring(3);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("自治区")){
                    address1 = address1.substring(3);
                }
                if(StringUtils.isNotBlank(address1)){
                    Matcher matcher1 = city_city_pattern.matcher(address1);
                    String cityStart = null;
                    if(matcher1.find()){
                        cityStart = matcher1.group();
                        String group = cityStart;
                        dto.setCity(group);
                    }
                    if(StringUtils.isNotBlank(cityStart) && cityStart.length() < address1.length()){
                        String address2 = address1.substring(cityStart.length());
                        if(StringUtils.isNotBlank(address2)){
                            Matcher matcher2 = country_city_pattern.matcher(address2);
                            String countyStart = null;
                            if(matcher2.find()){
                                countyStart = matcher2.group();
                                String group = countyStart;
                                dto.setCounty(group);
                            }
                            if(StringUtils.isNotBlank(countyStart)){
                                if(countyStart.length()<address2.length()){
                                    String address3 = address2.substring(countyStart.length());
                                    Matcher matcher3 = town_village_pattern.matcher(address3);
                                    while (matcher3.find()) {
                                        // 加入乡镇
                                        String town = matcher3.group("town");
                                        dto.setTown(town == null ? "" : town.trim());
                                        // 加入村庄
                                        String village = matcher3.group("village");
                                        dto.setVillage(village == null ? "" : village.trim());
                                        //详细地址
                                        String detailAddress = matcher3.group("address");
                                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                    }
                                }
                            }
                        }
                    }
                }
            } else if("西藏".equals(provinceStart) && address.length()>2){
                String address1 = address.substring(2);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("自治区")){
                    address1 = address1.substring(3);
                }
                if(StringUtils.isNotBlank(address1)){
                    Matcher matcher1 = city_city_pattern.matcher(address1);
                    String cityStart = null;
                    if(matcher1.find()){
                        cityStart = matcher1.group();
                        String group = cityStart;
                        dto.setCity(group);
                    }
                    if(StringUtils.isNotBlank(cityStart) && cityStart.length() < address1.length()){
                        String address2 = address1.substring(cityStart.length());
                        if(StringUtils.isNotBlank(address2)){
                            Matcher matcher2 = country_city_pattern.matcher(address2);
                            String countyStart = null;
                            if(matcher2.find()){
                                countyStart = matcher2.group();
                                String group = countyStart;
                                dto.setCounty(group);
                            }
                            if(StringUtils.isNotBlank(countyStart)){
                                if(countyStart.length()<address2.length()){
                                    String address3 = address2.substring(countyStart.length());
                                    Matcher matcher3 = town_village_pattern.matcher(address3);
                                    while (matcher3.find()) {
                                        // 加入乡镇
                                        String town = matcher3.group("town");
                                        dto.setTown(town == null ? "" : town.trim());
                                        // 加入村庄
                                        String village = matcher3.group("village");
                                        dto.setVillage(village == null ? "" : village.trim());
                                        //详细地址
                                        String detailAddress = matcher3.group("address");
                                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                    }
                                }
                            }
                        }
                    }
                }
            } else if("新疆".equals(provinceStart) && address.length()>2){
                // 新疆有直辖县需要特殊处理
                String address1 = address.substring(2);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("自治区")){
                    address1 = address1.substring(3);
                }else if(StringUtils.isNotBlank(address1) && address1.startsWith("维吾尔自治区")){
                    address1 = address1.substring(6);
                }
                if(StringUtils.isNotBlank(address1)){
                    Matcher matcher1 = xinJiang6590_pattern.matcher(address1);
                    String cityStart = "直辖县";
                    String cityStart0 = null;
                    if(matcher1.find()){
                        cityStart = matcher1.group();
                        if(StringUtils.isNotBlank(cityStart)){
                            String group = cityStart+"市";
                            dto.setCounty(group);
                        }else {
                            Matcher matcher0 = city_city_pattern.matcher(address1);
                            if(matcher0.find()){
                                cityStart0 = matcher0.group();
                                String group = cityStart0;
                                dto.setCity(group);
                            }
                        }
                    }else {
                        Matcher matcher0 = city_city_pattern.matcher(address1);
                        if(matcher0.find()){
                            cityStart0 = matcher0.group();
                            String group = cityStart0;
                            dto.setCity(group);
                        }
                    }
                    String city = dto.getCity();
                    String county = dto.getCounty();
                    if((StringUtils.isNotBlank(city) || StringUtils.isNotBlank(county))){
                        if(StringUtils.isNotBlank(city) && StringUtils.isBlank(county) && StringUtils.isNotBlank(cityStart0) && cityStart0.length()<address1.length()){
                            if(address1.startsWith("市")){
                                address1 = address1.substring(1);
                            }
                            String address2 = address1.substring(cityStart0.length());
                            Matcher matcher2 = country_city_pattern.matcher(address2);
                            String countyStart = null;
                            if(matcher2.find()){
                                countyStart = matcher2.group();
                                String group = countyStart;
                                if(StringUtils.isNotBlank(group)){
                                    if(address2.startsWith("辖区")){
                                        group = "市辖区";
                                    }
                                }
                                dto.setCounty(group);
                            }
                            if(StringUtils.isNotBlank(countyStart)){
                                if(countyStart.length()< address2.length()){
                                    String address3 = address2.substring(countyStart.length());
                                    Matcher matcher3 = town_village_pattern.matcher(address3);
                                    while (matcher3.find()) {
                                        // 加入乡镇
                                        String town = matcher3.group("town");
                                        dto.setTown(town == null ? "" : town.trim());
                                        // 加入村庄
                                        String village = matcher3.group("village");
                                        dto.setVillage(village == null ? "" : village.trim());
                                        //详细地址
                                        String detailAddress = matcher3.group("address");
                                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                    }
                                }
                            }
                        }else if(StringUtils.isBlank(city) && StringUtils.isNotBlank(county) && StringUtils.isNotBlank(cityStart) && cityStart.length()<address1.length()){
                            dto.setCity("直辖县");
                            String address3 = address1.substring(cityStart.length());
                            if(StringUtils.isNotBlank(address3) && address3.startsWith("市")){
                                address3 = address3.substring(1);
                            }
                            if(StringUtils.isNotBlank(address3) && address3.startsWith(dto.getCounty())){
                                address3 = address3.substring(dto.getCounty().length());
                            }
                            Matcher matcher3 = town_village_pattern.matcher(address3);
                            while (matcher3.find()) {
                                // 加入乡镇
                                String town = matcher3.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher3.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher3.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }
                    }
                }
            } else if("广西".equals(provinceStart) && address.length()>2){
                String address1 = address.substring(2);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("自治区")){
                    address1 = address1.substring(3);
                }else if(StringUtils.isNotBlank(address1) && address1.startsWith("壮族自治区")){
                    address1 = address1.substring(5);
                }
                if(StringUtils.isNotBlank(address1)){
                    Matcher matcher1 = city_city_pattern.matcher(address1);
                    String cityStart = null;
                    if(matcher1.find()){
                        cityStart = matcher1.group();
                        String group = cityStart;
                        dto.setCity(group);
                    }
                    if(StringUtils.isNotBlank(cityStart) && cityStart.length() < address1.length()){
                        String address2 = address1.substring(cityStart.length());
                        if(StringUtils.isNotBlank(address2)){
                            Matcher matcher2 = country_city_pattern.matcher(address2);
                            String countyStart = null;
                            if(matcher2.find()){
                                countyStart = matcher2.group();
                                String group = countyStart;
                                dto.setCounty(group);
                            }
                            if(StringUtils.isNotBlank(countyStart)){
                                if(countyStart.length()<address2.length()){
                                    String address3 = address2.substring(countyStart.length());
                                    Matcher matcher3 = town_village_pattern.matcher(address3);
                                    while (matcher3.find()) {
                                        // 加入乡镇
                                        String town = matcher3.group("town");
                                        dto.setTown(town == null ? "" : town.trim());
                                        // 加入村庄
                                        String village = matcher3.group("village");
                                        dto.setVillage(village == null ? "" : village.trim());
                                        //详细地址
                                        String detailAddress = matcher3.group("address");
                                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                    }
                                }
                            }
                        }
                    }
                }
            } else if("宁夏".equals(provinceStart) && address.length()>2){
                String address1 = address.substring(2);
                if(StringUtils.isNotBlank(address1) && address1.startsWith("自治区")){
                    address1 = address1.substring(3);
                }else if(StringUtils.isNotBlank(address1) && address1.startsWith("回族自治区")){
                    address1 = address1.substring(5);
                }
                if(StringUtils.isNotBlank(address1)){
                    Matcher matcher1 = city_city_pattern.matcher(address1);
                    String cityStart = null;
                    if(matcher1.find()){
                        cityStart = matcher1.group();
                        String group = cityStart;
                        dto.setCity(group);
                    }
                    if(StringUtils.isNotBlank(cityStart) && cityStart.length() < address1.length()){
                        String address2 = address1.substring(cityStart.length());
                        if(StringUtils.isNotBlank(address2)){
                            Matcher matcher2 = country_city_pattern.matcher(address2);
                            String countyStart = null;
                            if(matcher2.find()){
                                countyStart = matcher2.group();
                                String group = countyStart;
                                dto.setCounty(group);
                            }
                            if(StringUtils.isNotBlank(countyStart)){
                                if(countyStart.length()<address2.length()){
                                    String address3 = address2.substring(countyStart.length());
                                    Matcher matcher3 = town_village_pattern.matcher(address3);
                                    while (matcher3.find()) {
                                        // 加入乡镇
                                        String town = matcher3.group("town");
                                        dto.setTown(town == null ? "" : town.trim());
                                        // 加入村庄
                                        String village = matcher3.group("village");
                                        dto.setVillage(village == null ? "" : village.trim());
                                        //详细地址
                                        String detailAddress = matcher3.group("address");
                                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                // 海南、湖北、河南 - 直辖县
                if("海南".equals(provinceStart) && address.length()>2){
                    String address1 = address.substring(2);
                    if(StringUtils.isNotBlank(address1) && address1.startsWith("省")){
                        address1 = address1.substring(1);
                    }
                    if(StringUtils.isNotBlank(address1)){
                        Matcher matcher1 = haiNanCity_pattern.matcher(address1);
                        String cityStart0= null;
                        String countyStart0 = null;
                        if(matcher1.find()){
                            cityStart0 = matcher1.group();
                            if(StringUtils.isNotBlank(cityStart0)){
                                dto.setCity(cityStart0+"市");
                            }
                        }else {
                            countyStart0 = "直辖县";
//                            dto.setCounty("直辖县");
                        }
                        String city = dto.getCity();
//                        String county = dto.getCounty();
                        if((StringUtils.isNotBlank(city) || StringUtils.isNotBlank(countyStart0))){
                            if(StringUtils.isNotBlank(city) && StringUtils.isBlank(countyStart0) && StringUtils.isNotBlank(cityStart0) && cityStart0.length()<address1.length()){
                                String address2 = address1.substring(cityStart0.length());
                                if(address2.startsWith("市")){
                                    address2 = address2.substring(1);
                                    if(address2.startsWith("辖区")){
                                        dto.setCounty("市辖区");
                                        address2 = address2.substring(2);
                                    }else if(address2.startsWith("市辖区")){
                                        dto.setCounty("市辖区");
                                        address2 = address2.substring(3);
                                    }
                                }
                                Matcher matcher2 = country_city_pattern.matcher(address2);
                                String countyStart = null;
                                if(StringUtils.isBlank(dto.getCounty()) && matcher2.find()){
                                    countyStart = matcher2.group();
                                    String group = countyStart;
                                    if(StringUtils.isNotBlank(group)){
                                        if(address2.startsWith("辖区")){
                                            group = "市辖区";
                                        }
                                    }
                                    dto.setCounty(group);
                                    if(countyStart.length()< address2.length()){
                                        String address3 = address2.substring(countyStart.length());
                                        Matcher matcher6 = town_village_pattern.matcher(address3);
                                        while (matcher6.find()) {
                                            // 加入乡镇
                                            String town = matcher6.group("town");
                                            dto.setTown(town == null ? "" : town.trim());
                                            // 加入村庄
                                            String village = matcher6.group("village");
                                            dto.setVillage(village == null ? "" : village.trim());
                                            //详细地址
                                            String detailAddress = matcher6.group("address");
                                            dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                        }
                                    }
                                }else if(StringUtils.isNotBlank(dto.getCounty()) && dto.getCounty().startsWith("市辖区")){
                                    Matcher matcher6 = town_village_pattern.matcher(address2);
                                    while (matcher6.find()) {
                                        // 加入乡镇
                                        String town = matcher6.group("town");
                                        dto.setTown(town == null ? "" : town.trim());
                                        // 加入村庄
                                        String village = matcher6.group("village");
                                        dto.setVillage(village == null ? "" : village.trim());
                                        //详细地址
                                        String detailAddress = matcher6.group("address");
                                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                    }
                                }else {

                                }
                            }else if(StringUtils.isBlank(city) && StringUtils.isNotBlank(countyStart0) && StringUtils.isNotBlank(countyStart0) && countyStart0.length()<address1.length()){
                                dto.setCity("直辖县");
                                String address3 = address1;
                                if(StringUtils.isNotBlank(address3) && address3.startsWith("市")){
                                    address3 = address3.substring(1);
                                }
                                Matcher matcher2 = country_city_pattern.matcher(address3);
                                String countyStart = null;
                                if(matcher2.find()){
                                    countyStart = matcher2.group();
                                    dto.setCounty(countyStart);
                                }
                                Matcher matcher3 = town_village_pattern.matcher(address3);
                                while (matcher3.find()) {
                                    // 加入乡镇
                                    String town = matcher3.group("town");
                                    dto.setTown(town == null ? "" : town.trim());
                                    // 加入村庄
                                    String village = matcher3.group("village");
                                    dto.setVillage(village == null ? "" : village.trim());
                                    //详细地址
                                    String detailAddress = matcher3.group("address");
                                    dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                }

                            }
                        }
                    }
                }else if("湖北".equals(provinceStart) && address.length()>2){
                    String address1 = address.substring(2);
                    if(address1.startsWith("省")){
                        address1 = address1.substring(1);
                    }
                    Matcher matcher1 = huBeiCity_pattern.matcher(address1);
                    String cityStart = null;
                    String countyStart = null;
                    if (matcher1.find()){
                        cityStart = matcher1.group();
                        if(StringUtils.isNotBlank(cityStart)){
                            dto.setCity(cityStart+"市");
                        }
                    }
                    Matcher matcher2 = huBeiCounty4228_pattern.matcher(address1);
                    if (matcher2.find()){
                        countyStart = matcher2.group();
                        if(StringUtils.isNotBlank(countyStart)){
                            dto.setCounty(countyStart+"县");
                            dto.setCity("恩施土家族苗族自治州");
                        }
                    }
                    Matcher matcher3 = huBeiCounty4290_pattern.matcher(address1);
                    if (matcher3.find()){
                        countyStart = matcher3.group();
                        if(StringUtils.isNotBlank(countyStart)){
                            if("神农架".equals(countyStart)){
                                dto.setCounty(countyStart+"林区");
                            }else {
                                dto.setCounty(countyStart+"市");
                            }
                            dto.setCity("直辖县");
                        }
                    }
                    if(StringUtils.isBlank(dto.getCity()) && StringUtils.isBlank(dto.getCounty())){
                        Matcher matcher4 = huBeiCity4228_pattern.matcher(address1);
                        if (matcher4.find()){
                            countyStart = matcher4.group();
                            if(StringUtils.isNotBlank(countyStart)){
                                dto.setCounty(countyStart+"市");
                                dto.setCity("恩施土家族苗族自治州");
                            }
                        }
                    }
                    if(StringUtils.isBlank(dto.getCity()) && StringUtils.isBlank(dto.getCounty())){
                        // 此处一定是恩施市
                        dto.setCounty("恩施市");
                        dto.setCity("恩施土家族苗族自治州");
                        // 下面不要加 '市' 这个汉字
                        countyStart = "恩施";
                    }
                    if(StringUtils.isNotBlank(cityStart) && StringUtils.isBlank(countyStart) && cityStart.length() < address1.length()){
                        String address2 = address1.substring(cityStart.length());
                        if(address2.startsWith("市")){
                            address2 = address2.substring(1);
                            if(address2.startsWith("辖区")){
                                dto.setCounty("市辖区");
                                address2 = address2.substring(2);
                            }else if(address2.startsWith("市辖区")){
                                dto.setCounty("市辖区");
                                address2 = address2.substring(3);
                            }
                        }
                        Matcher matcher5 = country_city_pattern.matcher(address2);
                        String countyStart5 = null;
                        if(StringUtils.isBlank(dto.getCounty()) && matcher5.find()){
                            countyStart5 = matcher5.group();
                            String group = countyStart5;
                            if(StringUtils.isNotBlank(group)){
                                if(address2.startsWith("辖区")){
                                    group = "市辖区";
                                }
                            }
                            dto.setCounty(group);
                            String address3 = address2.substring(countyStart5.length());
                            Matcher matcher6 = town_village_pattern.matcher(address3);
                            while (matcher6.find()) {
                                // 加入乡镇
                                String town = matcher6.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher6.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher6.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }else if(StringUtils.isNotBlank(dto.getCounty()) && dto.getCounty().startsWith("市辖区")){
                            Matcher matcher6 = town_village_pattern.matcher(address2);
                            while (matcher6.find()) {
                                // 加入乡镇
                                String town = matcher6.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher6.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher6.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }
                    }else if(StringUtils.isBlank(cityStart) && StringUtils.isNotBlank(countyStart) && countyStart.length() < address1.length()){
                        String address2 = address1.substring(countyStart.length());
                        if(StringUtils.isNotBlank(address2) && address2.startsWith("土家族苗族自治州")){
                            address2 = address2.substring(8);
                        }else if(StringUtils.isNotBlank(address2) && address2.startsWith("家族苗族自治州")){
                            address2 = address2.substring(7);
                        }else if(StringUtils.isNotBlank(address2) && address2.startsWith("治州恩施市")){
                            address2 = address2.substring(5);
                        }else if(StringUtils.isNotBlank(address2) && address2.startsWith("自治州")){
                            address2 = address2.substring(3);
                        }else if(StringUtils.isNotBlank(address2) && address2.startsWith("林区")){
                            address2 = address2.substring(2);
                        }else if(StringUtils.isNotBlank(address2) && address2.startsWith("市")){
                            address2 = address2.substring(1);
                        }else if(StringUtils.isNotBlank(address2) && address2.startsWith("县")){
                            address2 = address2.substring(1);
                        }
                        if(StringUtils.isNotBlank(address2) && address2.startsWith(countyStart)){
                            if(!address2.startsWith("恩施")){
                                address2 = address2.substring(countyStart.length());
                            }
                            // 特殊处理神农架林区
                            if(address2.startsWith("林区")){
                                address2 = address2.substring(2);
                            }
                            if(StringUtils.isNotBlank(address2) && address2.startsWith("市")){
                                address2 = address2.substring(1);
                            }else if(StringUtils.isNotBlank(address2) && address2.startsWith("县")){
                                address2 = address2.substring(1);
                            }
                        }
                        Matcher matcher5 = town_village_pattern.matcher(address2);
                        while (matcher5.find()) {
                            // 加入乡镇
                            String town = matcher5.group("town");
                            dto.setTown(town == null ? "" : town.trim());
                            // 加入村庄
                            String village = matcher5.group("village");
                            dto.setVillage(village == null ? "" : village.trim());
                            //详细地址
                            String detailAddress = matcher5.group("address");
                            dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                        }
                    }
                }else if("河南".equals(provinceStart) && address.length()>2){
                    String address1 = address.substring(2);
                    if(address1.startsWith("省")){
                        address1 = address1.substring(1);
                    }
                    Matcher matcher1 = heNanCity_pattern.matcher(address1);
                    String cityStart = null;
                    if(matcher1.find()){
                        cityStart = matcher1.group();
                        if(StringUtils.isNotBlank(cityStart)){
                            dto.setCity(cityStart+"市");
                        }
                    }
                    if(StringUtils.isBlank(cityStart)){
                        dto.setCounty("济源市");
                    }
                    if(StringUtils.isNotBlank(cityStart) && StringUtils.isBlank(dto.getCounty()) && cityStart.length()<address1.length()){
                        String address2 = address1.substring(cityStart.length());
                        if(StringUtils.isNotBlank(address2) && address2.startsWith("市")){
                            address2 = address2.substring(1);
                            if(address2.startsWith("辖区")){
                                dto.setCounty("市辖区");
                                address2 = address2.substring(2);
                            }else if(address2.startsWith("市辖区")){
                                dto.setCounty("市辖区");
                                address2 = address2.substring(3);
                            }
                        }
                        Matcher matcher5 = country_city_pattern.matcher(address2);
                        String countyStart5 = null;
                        if(StringUtils.isBlank(dto.getCounty()) && matcher5.find()){
                            countyStart5 = matcher5.group();
                            String group = countyStart5;
                            if(StringUtils.isNotBlank(group)){
                                if(address2.startsWith("辖区")){
                                    group = "市辖区";
                                }
                            }
                            dto.setCounty(group);
                            if(countyStart5.length()< address2.length()){
                                String address3 = address2.substring(countyStart5.length());
                                Matcher matcher6 = town_village_pattern.matcher(address3);
                                while (matcher6.find()) {
                                    // 加入乡镇
                                    String town = matcher6.group("town");
                                    dto.setTown(town == null ? "" : town.trim());
                                    // 加入村庄
                                    String village = matcher6.group("village");
                                    dto.setVillage(village == null ? "" : village.trim());
                                    //详细地址
                                    String detailAddress = matcher6.group("address");
                                    dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                }
                            }
                        } else if(StringUtils.isNotBlank(dto.getCounty()) && dto.getCounty().startsWith("市辖区")){
                            Matcher matcher6 = town_village_pattern.matcher(address2);
                            while (matcher6.find()) {
                                // 加入乡镇
                                String town = matcher6.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher6.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher6.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }else {
//                            if(countyStart5.length()< address2.length()){
//                                String address3 = address2.substring(countyStart5.length());
//                                Matcher matcher6 = town_village_pattern.matcher(address3);
//                                while (matcher6.find()) {
//                                    // 加入乡镇
//                                    String town = matcher6.group("town");
//                                    dto.setTown(town == null ? "" : town.trim());
//                                    // 加入村庄
//                                    String village = matcher6.group("village");
//                                    dto.setVillage(village == null ? "" : village.trim());
//                                    //详细地址
//                                    String detailAddress = matcher6.group("address");
//                                    dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
//                                }
//                            }
                        }
                    }else if(StringUtils.isBlank(cityStart) && StringUtils.isNotBlank(dto.getCounty()) && dto.getCounty().length() < address1.length()){
                        String address2 = address1.substring(2);
                        if(StringUtils.isNotBlank(address2) && address2.startsWith("市")){
                            address2 = address2.substring(1);
                        }
                        dto.setCity("直辖县");
                        if(StringUtils.isNotBlank(address2)){
                            if(address2.startsWith("县")){
                                address2 = address2.substring(1);
                                if(address2.startsWith("济源")){
                                    address2 = address2.substring(2);
                                }
                                if(address2.startsWith("市")){
                                    address2 = address2.substring(1);
                                }
                            }
                            Matcher matcher3 = town_village_pattern.matcher(address2);
                            while (matcher3.find()) {
                                // 加入乡镇
                                String town = matcher3.group("town");
                                dto.setTown(town == null ? "" : town.trim());
                                // 加入村庄
                                String village = matcher3.group("village");
                                dto.setVillage(village == null ? "" : village.trim());
                                //详细地址
                                String detailAddress = matcher3.group("address");
                                dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                            }
                        }
                    }
                }else {
                    // 以'省'结尾的省份，且无特殊行政区划，统一处理
                    if(address.length()>2){
                        // 无特殊情况，可以直接匹配了
                        String address1 = address.substring(provinceStart.length());
                        System.out.println("aadfsfsfd "+address1);
                        if(address1.startsWith("省")){
                            address1 = address1.substring(1);
                        }
                        Matcher matcher1 = city_city_pattern.matcher(address1);
                        String cityStart = null;
                        if(matcher1.find()){
                            cityStart = matcher1.group();
                            dto.setCity(cityStart);
                        }
                        String countyStart = null;
                        if(StringUtils.isNotBlank(cityStart) && cityStart.length() < address1.length()){
                            String address2 = address1.substring(cityStart.length());
                            Matcher matcher2 = country_city_pattern.matcher(address2);
                            if(matcher2.find()){
                                countyStart = matcher2.group();
                                dto.setCounty(countyStart);
                                if(StringUtils.isNotBlank(countyStart)){
                                    if(countyStart.startsWith("辖区")){
                                        dto.setCounty("市辖区");
                                    }
                                }
                            }
                            if(StringUtils.isNotBlank(countyStart) && countyStart.length() < address2.length()){
                                String address3 = address2.substring(countyStart.length());
                                Matcher matcher3 = town_village_pattern.matcher(address3);
                                while (matcher3.find()) {
                                    // 加入乡镇
                                    String town = matcher3.group("town");
                                    dto.setTown(town == null ? "" : town.trim());
                                    // 加入村庄
                                    String village = matcher3.group("village");
                                    dto.setVillage(village == null ? "" : village.trim());
                                    //详细地址
                                    String detailAddress = matcher3.group("address");
                                    dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                                }
                            }
                        }
                    }
                }
            }
        }else {
            // 没有填写省（自治区/直辖市/特别行政区）的字符串。从市开始匹配。
            Matcher matcher1 = city_city_pattern.matcher(address);
            String cityStart = null;
            if(matcher1.find()){
                cityStart = matcher1.group();
                dto.setCity(cityStart);
            }
            if(StringUtils.isNotBlank(cityStart) && cityStart.length() < address.length()){
                String address1 = address.substring(cityStart.length());
                Matcher matcher2 = country_city_pattern.matcher(address1);
                String countyStart = null;
                if(matcher2.find()){
                    countyStart = matcher2.group();
                    dto.setCounty(countyStart);
                    if(StringUtils.isNotBlank(countyStart)){
                        if(countyStart.startsWith("辖区")){
                            dto.setCounty("市辖区");
                        }
                    }
                }
                if (StringUtils.isNotBlank(countyStart) && countyStart.length() < address1.length()){
                    String address2 = address1.substring(countyStart.length());
                    Matcher matcher3 = town_village_pattern.matcher(address2);
                    while (matcher3.find()) {
                        // 加入乡镇
                        String town = matcher3.group("town");
                        dto.setTown(town == null ? "" : town.trim());
                        // 加入村庄
                        String village = matcher3.group("village");
                        dto.setVillage(village == null ? "" : village.trim());
                        //详细地址
                        String detailAddress = matcher3.group("address");
                        dto.setDetailAddress(detailAddress == null ? "" : detailAddress.trim());
                    }
                }
            }
        }
        return dto;
    }


}
